<template lang="html">
  <div class="home">

    <!-- <div class='clearfix'>
      <el-button type="danger" class='pull-right mb10' icon="el-icon-delete" style='margin-right:10px;' @click="tourl('/content/trash')">回收箱</el-button>
    </div> -->

    <div class='searchView'>
      <el-form :inline="true" class="demo-form-inline">
        
<!--         <el-form-item label="">
          <el-input placeholder="输入标题或书名…"></el-input>
        </el-form-item> -->
        <el-form-item label="">
          <el-select placeholder="请选择类型" v-model='status'>
            <el-option :label="i.label" :value="i.value" v-for='(i,index) in options' :key='index'></el-option>
          </el-select>
        </el-form-item>
<!--         <el-form-item label="">
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item> -->
        
        <el-form-item>
          <el-button type="primary" >查询</el-button>
        </el-form-item>

<!--         <el-form-item class='pull-right'>
          <el-button type="primary">首推区</el-button>
        </el-form-item> -->
      </el-form>
    </div>

    <div class='tableView'>
      <el-tag type="info" class='mb10'>共{{totalData}}条记录</el-tag>
      <el-table
        ref="multipleTable"
        :data="tableData"
        v-loading='loading'
        align='center'
        border
        height='500'
        tooltip-effect="dark">

        <el-table-column
        align='center'
        label="类别">
          <template slot-scope="scope">
            {{status == 1?"文章":"书籍"}}
          </template>
        </el-table-column>

        <el-table-column
        align='center'
        label="标题/书名">
          <template slot-scope="scope">
            {{scope.row.title || "-"}}
          </template>
        </el-table-column>

        <el-table-column
        align='center'
        label="简介">
          <template slot-scope="scope">
            {{scope.row.introduce || "-"}}
          </template>
        </el-table-column>

        <el-table-column
        align='center'
        width='100'
        prop='createTime'
        label="发表时间">
        </el-table-column>

        <el-table-column
        align='center'
        prop='viewCount'
        label="阅读数">
        </el-table-column>

        <el-table-column
        align='center'
        prop='favorityCount'
        label="收藏数">
        </el-table-column>

        <el-table-column
        align='center'
        prop='commentCount'
        label="评论数">
        </el-table-column>

        <el-table-column
        align='center'
        prop='likeCount'
        label="点赞数">
        </el-table-column>

        <el-table-column
        label=""
        width='220'
        align='center'>
          <template slot-scope="scope">
            <el-button size='mini' v-if='!scope.row.isRecommend' type="success" @click='delIt(scope.row.id,1)'>首推</el-button>
            <el-button size='mini' v-if='scope.row.isRecommend' type="danger" @click='delIt(scope.row.id,0)'>取消首推</el-button>
          </template>
        </el-table-column>

      </el-table>

      <div class="pageBlock">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper, sizes"
          :total="totalData">
        </el-pagination>
      </div>
    </div>

  </div>
</template>
<script>
export default {

  data () {
    return {
      tableData:[],
      loading:false,
      status:'',
      date:'',
      options: [
        {
          value: 0,
          label: '文章'
        },
        {
          value: 1,
          label:'书籍'
        }
      ],
      totalData:0,
      pageSize:10,
      currentPage:1
    }
  },
  watch:{
    status(val){
      this.getList(1);
    }
  },
  methods:{
    delIt(id,type){
      this.$confirm('是否确认操作？',{center:true})
      .then(res =>{

        let url = '';
        if(type == 1){
          url = '/v1/article/addUtterRecommendInfo';
        }else{
          url = '/v1/article/deleteUtterRecommendInfo';
        }

        this.loading = true;
        tools.ajax({
            url,
            data:{
              type:this.status,
              infoId:id
            },
            type:'POST'
        })
        .then(res => {
          this.loading = false;
          if(res.code == 200){
            tools.toastOk('操作成功');
            this.getList(1);
          }
        })
        .catch(e => {
          this.loading = false;
        })

      }).catch( e=> {})
    },
    create(){
      if(!this.searchUserId){
        tools.toastWarn('请选择用户');
        return
      };

      this.tourl('/content/user/create',{userId:this.searchUserId});
    },
  	tourl(url,query){
      tools.router.push({path:url,query:query || {}})
    },
    getList(page){
      if(page){
        this.currentPage = page;
      }

      let url = '';
      if(this.status == 0){
        url = '/v1/article/getArticleList';
      }else{
        url = '/v1/book/getBookList';
      };

      this.loading = true;
      tools.ajax({
        url,
        data:{
          page:this.currentPage,
          pageSize:this.pageSize
        },
        type:'POST'
      })
      .then(res => {
        this.loading = false;
        if(res.code == 200){
          this.tableData = res.data.rows;
          this.totalData = +res.data.total;
        }
      })
      .catch(e => {
        this.loading = false;
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.getList();
    }
  },
  mounted(){
    var _this = this;
    // this.getList();
  },
  components:{
  }
}
</script>

<style scoped lang='scss'>
  @import '../../../assets/scss/tool.scss';
  .home{
    
  }
</style>